﻿<?php
include_once("../initial.php");
include_once("../dbdriver/deviceWHandle.php");
include_once("../dbdriver/deviceRHandle.php");
include_once("../dbdriver/categoryRHandle.php");
class deviceLogic
{
    public function insert_device($array)
    {
        if(is_array($array)==false)
        {
            return false;
        }
        if(count($array)==0)
        {
            return false;
        }
        $whandle = new deviceWHandle();
        return $whandle->insert_data($array);
    }
    public function get_device($array='',$orderby="",$limit="")
    {
        $rhandle = new deviceRHandle();
        if($array == '')
        {
            return $rhandle->get_all_data($orderby,$limit);
        }
        else
        {
            return $rhandle->get_some_data($array,$orderby,$limit);
        }
    }
    public function get_device_num($array='')
    {
        $rhandle = new deviceRHandle();
        return $rhandle->get_device_num($array);
    }
    public function update_device($array,$id)
    {
        $handle = new deviceWHandle();
        return $handle->update_device($array,$id);
    }
    public function delete_device($id)
    {
        $handle = new deviceWHandle();
        return $handle->delete_device($id);
    }
    public function get_categories($deviceId)
    {
    		if($deviceId=='')
    		{
    			return array();
    		}
    		$cr=new categoryRHandle();
    		$categoryIds=$cr->get_category_devices(array("did"=>$deviceId), "order by createtime desc");
    		//填充每一个分类的信息
    		if(!is_array($categoryIds) || count($categoryIds)<=0)
    		{
    				return array();
    		}
    		for($i=0; $i<count($categoryIds); $i++)
    		{
    				$tmp=$cr->get_category_by_id($categoryIds[$i]['cid']);
    				if(!is_array($tmp) || count($tmp)<=0)
    				{
    					continue;
    					}
    				$categoryIds[$i]['name']=$tmp[0]['name'];
    		}
    		return $categoryIds;
    }
    public function insert_device_picture($deviceId, $pictureName, $notice='', $createtime='')
    {
    		if($createtime=='')
    		{
    				$createtime=date("Y-m-d H:i:s");
    		}
    		$array=array("did"=>$deviceId, "picture_name"=>$pictureName, "picture_note"=>$notice, "createtime"=>$createtime);
    		$handle = new deviceWHandle();
    		return $handle->insert_device_picture($array);
    }
    public function get_device_pictures($deviceId)
    {
    	$handle=new deviceRHandle();
    	return $handle->get_device_pictures(array("did"=>$deviceId), "order by createtime desc");
    }
    public function remove_device_picture($pid)
    {
    		if($pid=='')
    		{
    				return false;
    		}
    		$handle = new deviceWHandle();
    		return $handle->remove_device_picture($pid);
    }
    public function delete_device_course($did, $cid)
    {
        $handle = new deviceWHandle();
        $condition=array("did"=>$did, "cid"=>$cid);
        return $handle->delete_device_course($condition);
    }
    public function get_device_courses($did)
    {
        $rhandle = new deviceRHandle();
        $array=array("did"=>$did);
        return $rhandle->get_device_courses($array, 'order by createtime desc');
    }
    public function insert_device_course($did, $cid, $createtime='')
    {
    		if($did=='' || $cid=='')
        {
        		return false;
        }
        $handle = new deviceWHandle();
        if($createtime=='')
        {
        		$createtime=date("Y-m-d H:i:s");
        }
        $array = array("did"=>"$did", "cid"=>"$cid", "createtime"=>"$createtime");
        return $handle->insert_device_course($array);
    }
    public function update_device_picture($pictureName, $note, $pid)
    {
    		if($pid=='')
    		{
    				return false;
    		}
    		$dw = new deviceWHandle();
    		$array=array();
    		if($pictureName!='')
    		{
    				$array['picture_name']=$pictureName;
    		}
    		if($note!='')
    		{
    				$array['picture_note']=$note;
    		}
    		return $dw->update_device_picture($array, $pid);
    }
    public function searchDevice($keyword)
    {
    		if(strlen($keyword)<3)
    		{
    				return array();
    		}
    		$cr = new deviceRHandle();
    		return $cr->searchDevice($keyword, "limit 0, 30");
    }
}
?>
